/**
 * 
 */
package com.antilia.infomaresme.pages.alert.edit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.EnumDropDownChoice;
import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dataobjects.Frequency;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemEventSubject;
import com.antilia.infomaresme.data.dataobjects.UserItemAlert;
import com.antilia.infomaresme.form.ShinyForm;
import com.antilia.infomaresme.pages.RoundSubmitLinkPanel;

/**
 * @author Ernesto
 *
 */
public abstract class EditItemUserAlertPanel extends Panel {

	private static final long serialVersionUID = 1L;


	private UserItemAlert alert;
	
	private List<ItemEventSubject> events;
	
	/**
	 * @param id
	 * @param model
	 */
	public EditItemUserAlertPanel(String id, IModel<Item> model, UserItemAlert alert) {
		super(id, model);
		this.alert = alert;
		if(this.alert == null) {
			this.alert = new UserItemAlert();
			this.alert.setUser(InfomaresmeSession.getSession().getUser());
			this.alert.setItem(model.getObject());
			if(model.getObject() != null) {
				this.alert.setTitle(model.getObject().getTitle());
			}
			this.events = new ArrayList<ItemEventSubject>();
		} else {
			this.events = alert.getEventsAsEnum();			
		}
		
		List<ItemEventSubject> EVENTS = Arrays.asList(ItemEventSubject.values());
		final Form<UserItemAlert> form = new ShinyForm<UserItemAlert>("form", new CompoundPropertyModel<UserItemAlert>(this.alert));
		add(form);
		
		form.add(new TextField<String>("title").setRequired(true));				
		form.add(new EnumDropDownChoice<Frequency>("emailFrequency", Frequency.class).setRequired(true));
		
		
		CheckBoxMultipleChoice<ItemEventSubject> events = new CheckBoxMultipleChoice<ItemEventSubject>("events", EVENTS);
		events.setChoiceRenderer(new EnumChoiceRenderer<ItemEventSubject>(this));
		events.setRequired(true);
		events.setDefaultModel(new PropertyModel<ItemEventSubject>(this, "events"));
		form.add(events);
		
		RoundSubmitLinkPanel save = new RoundSubmitLinkPanel("save", new ResourceModel("actions.createNewAlerta")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				try {
					EditItemUserAlertPanel.this.alert.setEvents(EditItemUserAlertPanel.this.events);
					InfomaresmeApplication.getQAApplication().getAlertsDao().save(EditItemUserAlertPanel.this.alert);					
					onSaved(target, form);
				} catch (org.hibernate.exception.ConstraintViolationException e) {
					form.error(getString("errors.itemAlertExist"));
					target.add(form);
				}
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {				
				target.add(form);
			}
		};
		form.add(save);
	}
	
	public abstract void onSaved(AjaxRequestTarget target, Form<?> form);
	
	public List<ItemEventSubject> getEvents() {
		return events;
	}
	public void setEvents(List<ItemEventSubject> events) {
		this.events = events;
	}

}
